package es.xpt.ysf.view.collections;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

import es.xpt.ysf.utils.ServiceLocatorException;

@XmlAccessorType(XmlAccessType.FIELD)
public class CollectionInfo {
	
	public static enum CollectionType { // NO_UCD 
		ENUMERATION, SERVICE, STATIC 
	};
	
	@XmlAttribute
	private String name;
	private String enumeration;
	private ResolvedMethod method;
	

	public CollectionInfo() {
		super();
	}
	
	/**
	 * @return
	 */
	public List<?> resolveEnumeration() throws ServiceLocatorException {
		
		try {
			Class<?> c = Class.forName(enumeration, true, Thread.currentThread().getContextClassLoader());
			if ( ! c.isEnum() ) {
				throw new ServiceLocatorException("Class " + enumeration + " does not represents an enumeration.");
			}
			return java.util.Arrays.asList(c.getEnumConstants());
		} catch (ClassNotFoundException e) {
			throw new ServiceLocatorException("Class " + enumeration + " not found.");
		}
		
	}
	
	public CollectionType getType() {
		if ( enumeration != null ) return CollectionType.ENUMERATION;
		else if ( method.getJndiName()!=null && !method.getJndiName().isEmpty())
			return CollectionType.SERVICE;
		else 
			return CollectionType.STATIC;
	}
	
		
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the enumeration
	 */
	public String getEnumeration() {
		return enumeration;
	}

	/**
	 * @param enumeration the enumeration to set
	 */
	public void setEnumeration(String enumeration) {
		this.enumeration = enumeration;
	}

	
	/**
	 * @return the method
	 */
	public ResolvedMethod getMethod() {
		return method;
	}

	/**
	 * @param method the method to set
	 */
	public void setMethod(ResolvedMethod method) {
		this.method = method;
	}


	
}
